package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.api.entity.MessageBean;
import com.qiantu.api.utils.ThemeUtil;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.glide.GlideApp;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 状态数据列表
 */
public final class MessageAdapter extends AppAdapter<MessageBean.ListDTO> {

    public MessageAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView iconMessage;
        private final TextView tvTimeDay;
        private final TextView tvTypeName;
        private final TextView tvTimeHour;
        private final TextView tvMessage;

        private ViewHolder() {
            super(R.layout.item_message);
            iconMessage = findViewById(R.id.icon_message);
            tvTimeDay = findViewById(R.id.tv_time_day);
            tvTypeName = findViewById(R.id.tv_type_name);
            tvTimeHour = findViewById(R.id.tv_time_hour);
            tvMessage = findViewById(R.id.tv_message);
        }

        @Override
        public void onBindView(int position) {
            String lastTimeDay = null;
            if(position > 0){
                MessageBean.ListDTO last = getItem(position - 1);
                String[] lastTime = last.getTime().split(" ");
                lastTimeDay = lastTime[0];
            }
            MessageBean.ListDTO current = getItem(position);
            String[] time = current.getTime().split(" ");
            String timeDay = time[0];
            String timeHour = time[1];
            if(!TextUtils.isEmpty(lastTimeDay) && lastTimeDay.equals(timeDay)){
                tvTimeDay.setVisibility(View.GONE);
            }else{
                tvTimeDay.setVisibility(View.VISIBLE);
                String[] ymd = timeDay.split("/");
                if(ymd.length > 2){
                    tvTimeDay.setText(ymd[1] + "月" + ymd[2] + "日");
                }
            }
            String[] hms = timeHour.split(":");
            if(hms.length > 1){
                tvTimeHour.setText(hms[0] + ":" + hms[1]);
            }
            tvTypeName.setVisibility(View.VISIBLE);
            tvTypeName.setText(current.getSubTitle());
//            if(current.getIcon().contains("http")){
//                GlideApp.with(iconMessage)
//                        .load(current.getIcon())
//                        .into(iconMessage);
//            }else{
                /**
                 * 全部 ： 0
                 * 场景任务任务 : 1001
                 * 联动任务执行 : 1101
                 * 设备状态提醒 : 1201
                 * 家庭消息推送 : 1301
                 * 设备异常 : 2001
                 * 环境异常 : 2101
                 * 系统消息通知 : 3001
                 * 房屋过户 : 3101
                 */
                switch (current.getSubCategory()){
                        case 1001:
                            String staticImage = current.getIcon();
                            String url = BuildConfig.APP_SCENE_BASE + ThemeUtil.getIconThemeSuffix() + staticImage + ".png";
                            GlideApp.with(iconMessage).load(url).into(iconMessage);
//                            iconMessage.setImageDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_scenes));
                            break;
                        case 1101:
                            iconMessage.setImageDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_linkage));
                            break;
                        case 1201:
                            break;
                        case 1301:
                            iconMessage.setImageDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_intelligent_action_push));
                            break;
                        case 2001:
                        case 2101:
                            iconMessage.setImageDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_news_alarm));
                        break;
                }
//                String iconStr = current.getIcon();
//                int icon = AppApplication.getInstance().getResourceId(iconStr, "attr");
//                if(icon != 0){
//                    iconMessage.setImageDrawable(AppApplication.getInstance().getThemeDrawable(icon));
//                }else{
//                    GlideApp.with(iconMessage)
//                            .load(BuildConfig.APP_SCENE_BASE + iconStr)
//                            .into(iconMessage);
//                }
//            }
            tvMessage.setText(current.getContent());
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }
}